package com.ruiysoft.controller.integral;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruiysoft.controller.integral.vo.IntegralConfVo;
import com.ruiysoft.entity.integral.IntegralConf;
import com.ruiysoft.entity.sys.SysMicroShop;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultCode;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.service.integral.IIntegralConfService;
import com.ruiysoft.service.sys.clent.LoginContextUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-04-13T19:28:15.988+08:00")

@RestController
public class IntegralConfApiController implements IntegralConfApi {

    private static final Logger log = LoggerFactory.getLogger(IntegralConfApiController.class);


    private final IIntegralConfService integralConfService;

    @org.springframework.beans.factory.annotation.Autowired
    public IntegralConfApiController(IIntegralConfService integralConfService) {
        this.integralConfService = integralConfService;
    }

    private Long getShopId() {
        SysMicroShop shop = LoginContextUtil.instance().getEntryMicroShop();
        return shop.getId();
    }

    @Override
    public Result<IntegralConfVo> integralConfGet() {
        Long shopId = getShopId();
        try {
            IntegralConf conf = integralConfService.getById(shopId);
            return ResultGenerator.genSuccessResult(new IntegralConfVo()
                    .rate(Objects.requireNonNull(conf,"未设置汇率").getRate()));
        } catch (Exception e) {
            log.error("商铺查询积分汇率--{}",shopId, e);
            return ResultGenerator.genFailResult(ResultCode.CODE_COMMON_ERROR);
        }
    }

    @Override
    public Result<Void> integralConfSavePost(@ApiParam(value = ""  )  @Valid @RequestBody IntegralConfVo body) {
        Long shopId = getShopId();
        try{
            IntegralConf conf = integralConfService.getOne(new QueryWrapper<IntegralConf>().eq("shop_id",shopId));
            Integer rate = Objects.requireNonNull(Objects.requireNonNull(body,"未设置汇率").getRate(),"未设置汇率");
            if(conf!=null){
                integralConfService.updateById(conf.setRate(rate));
            }else{
                try{
                    integralConfService.save(new IntegralConf().setShopId(shopId).setRate(rate));
                }catch (Exception e){
                    integralConfService.updateById(conf.setRate(rate));
                }
            }
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            log.error("商铺设置积分汇率--{}--{}",shopId,JSONObject.toJSONString(body), e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

}
